/**
 * Created by wzh on 2016/8/6.
 */
'use strict'
swan_pc.controller("BusinessCompanyManageController",function($scope,$rootScope,OrgCompany,OrgContact,OrgOpp,ngDialog,Address,$q,Department){
    $scope.getPages();
    $scope.editItemDetail=function(id){
        OrgCompany.queryAll({infoid:id},function(result){
            $scope.presentOrgCompany=result;
            $scope.orgCompanyPointer={
                companyBaseInfo:result.companyBaseInfo,
                companyAddressList:result.addresss,
                companyPhoneList:result.lineNumbers,
                companyDepartmentList:result.departments
            }
            ngDialog.open({
                templateUrl:"scripts/components/MyOrganization/businessInfo/businessCompanyManage/businessCompanyDetail.html",
                scope:$scope,
                controller:["$scope",function($scope){
                    $scope.module=$scope.orgCompanyStruct[0].blockName
                    /*$scope.checkIfSon=function(item1,item2){
                        var rt=false;
                        if (item1.parentDepartmentId==item2.id) rt=true;
                        else if(item2.subDepartments!=undefined&&item2.subDepartments.length>0) {
                            _.forEach(item2.subDepartments,function(item){
                                rt=rt|$scope.checkIfSon(item1,item);
                            })
                        }
                        return rt;
                    }
                    $scope.getDepartmentById=function(id){
                        var dep;
                        _.forEach($scope.orgCompanyPointer.companyDepartmentList, function (item) {
                            if (id == item.id) dep = item;
                        })
                        return dep;
                    }
                    $scope.updateOrgCompany=function(){
                        OrgCompany.update({},$scope.presentOrgCompany,function(result){
                            var df0=$q.defer();
                            if ($scope.presentOrgCompany.departments){
                                var error=false
                                _.forEach($scope.presentOrgCompany.departments,function(item){
                                    error=error||item.parentDepartmentId&&$scope.checkIfSon($scope.getDepartmentById(item.parentDepartmentId),item);
                                })
                                if (error) {
                                    alert("无法将部门的下级部门设置为其上级部门");
                                    return false;
                                }
                                var total=$scope.presentOrgCompany.departments.length;
                                var resolve=0;
                                _.forEach($scope.presentOrgCompany.departments,function(item){
                                    if (item.id) {
                                        Department.update({}, item, function () {
                                            resolve = resolve + 1;
                                            if (resolve == total) df0.resolve();
                                        }, function () {
                                            df0.reject();
                                        })
                                    }
                                    else{
                                        item.orgCompanyId=$scope.presentOrgCompany.id;
                                        Department.save({}, item, function () {
                                            resolve = resolve + 1;
                                            if (resolve == total) df0.resolve();
                                        }, function () {
                                            df0.reject();
                                        })
                                    }
                                })
                            }
                            else df0.resolve();
                            df0.promise.then(function(){
                                var df=$q.defer();
                                if ($scope.presentOrgCompany.addresss){
                                    var total=$scope.presentOrgCompany.addresss.length;
                                    var resolve=0;
                                    _.forEach($scope.presentOrgCompany.addresss,function(item){
                                        if (item.id) {
                                            Address.update({}, item, function () {
                                                resolve = resolve + 1;
                                                if (resolve == total) df.resolve();
                                            }, function () {
                                                df.reject();
                                            })
                                        }
                                        else{
                                            item.orgCompanyId=$scope.presentOrgCompany.id;
                                            Address.save({}, item, function () {
                                                resolve = resolve + 1;
                                                if (resolve == total) df.resolve();
                                            }, function () {
                                                df.reject();
                                            })
                                        }
                                    })
                                }
                                else df.resolve();
                                df.promise.then(function(){
                                    alert("保存成功");
                                    $scope.edit=false;
                                    $scope.resetOrgCompany(result.id)
                                },function(){
                                    alert("保存失败");
                                })
                            },function(){
                                alert("保存失败")
                            })
                        })
                    }
                    $scope.resetOrgCompany=function(id){
                        OrgCompany.queryAll({infoid: id}, function (result) {
                            $scope.presentOrgCompany = result;
                            $scope.orgCompanyPointer={
                                companyBaseInfo:result.companyBaseInfo,
                                companyAddressList:result.addresss,
                                companyPhoneList:result.lineNumbers,
                                companyDepartmentList:result.departments
                            }
                        })
                    }
                    $scope.addElement=function(array){
                        array.push({});
                    }
                    $scope.deleteDepartment=function(item){
                        if(item.subDepartments!=undefined&&item.subDepartments.length>0)
                            if(!confirm("删除此部门会将所有的下属部门变为顶级部门，确定要继续么?")) return;
                        var df=$q.defer();
                        var total=item.subDepartments.length;
                        var resolve=0;
                        _.forEach(item.subDepartments,function(dep){
                            dep.parentDepartmentId=null;
                            Department.update({},dep,function(){
                                resolve=resolve+1;
                                if (resolve==total) df.resolve()
                            },function(reason){
                                df.reject(reason);
                            })
                        })
                        df.promise.then(function(){
                            Department.delete({infoid:item.id},function(){
                                alert("删除成功")
                                $scope.loadDepartment();
                            },function(){
                                alert("删除失败")
                            })
                        },function(){
                            alert("子部门设置失败")
                        });
                    }
                    $scope.edit=false;
                    $scope.module=$scope.orgCompanyStruct[0].blockName
                    //脏检查
                    $scope.dirty={
                        companyBaseInfo:false,
                        companyAddressList:false,
                        companyPhoneList:false,
                    }
                    $scope.initDirty=function() {
                        $rootScope.orgCompanyDirty = false;
                        $scope.dirty.companyBaseInfo=false;
                        $scope.dirty.companyAddressList=false;
                        $scope.dirty.companyPhoneList=false;
                    }
                    $scope.dirtySet=function(index){
                        $rootScope.orgCompanyDirty=true;
                        $scope.dirty[index]=true;
                    }
                    $scope.dirtyGet=function(index){
                        return $scope.dirty[index];
                    }
                    //筛选
                    $scope.noSelect=function(item){
                        if (item.type=='select') return false;
                        return true;
                    }
                    $scope.depFilter={
                        department:null
                    }
                    $scope.ifSon=function(item){
                        if (!item.id) return false;
                        if ($scope.depFilter.department&&item.id==$scope.depFilter.department.id) return false;
                        //if ($scope.depFilter.department&&$scope.checkIfSon(item,$scope.depFilter.department)) return false;
                        return true;
                    }
                    //部门
                    $scope.getDepartmentNameById=function(id) {
                        var name = "无";
                        _.forEach($scope.orgCompanyPointer.companyDepartmentList, function (item) {
                            if (id == item.id) name = item.name;
                        })
                        return name;
                    }*/
                }]
            })
        },function(){
            $scope.presentOrgCompany=undefined;
        })
    }
})